package com.sso.oa.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sso.common.cons.Const;
import org.sso.common.cookie.TokenHelper;
import org.sso.common.domian.Ticket;
import org.sso.common.domian.Token;
import org.sso.common.encrypt.AES;

import com.sso.oa.cons.OaConst;

@Controller
public class OAController extends Base {

	@RequestMapping("/oa")
	public String login() {
		return "index";
	}

	/**
	 * 代理登录，接收cas服务器返回ticket，并且生成本地token
	 * 
	 * @return
	 */
	@RequestMapping("/proxy")
	public String proxy() {
		String data = request.getParameter("data");
		Ticket localticket = TokenHelper.getTicket(request);
		if (localticket == null || StringUtils.isBlank(data)) {
			return "notLogin";
		}
		Ticket tic = TokenHelper.replyCiphertext(request, data);
		if (tic != null && tic.getUuid().equals(localticket.getUuid())) {
			if (tic.verify(OaConst.RSA_SSO_PUBLIC_KEY) != null) {
				// 销毁临时cookie
				TokenHelper.clearCookieByName(request, response, Const.ticket_cookiename, OaConst.ticket_domian,
						Const.cookie_path);
				// 生成身份ticket
				Token token = tic.getTk();
				TokenHelper.setSSOCookie(request, response, OaConst.ticket_domian, token, AES.getInstance());
				return redirectTo("/oa");
			}
		}

		return "proxy";
	}
}
